using System.Windows.Markup;
using KinonekoSoftware.CodariaDB.Documents.Templates;
using KinonekoSoftware.Foundation.UI.Controls;
using KinonekoSoftware.Foundation.UI.Converters;

namespace KinonekoSoftware.Studio.Resources.Converters
{

    public sealed class FindDataPartGeometryConverter : ConverterBase
    {
        private static readonly Dictionary<DataPartType, string> _IconDictionary = new Dictionary<DataPartType, string>();
        private static          ResourceDictionary               _Icon;
        private static          Geometry                         _setting;


        public override object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (_Icon is null)
            {
                var uri = new Uri("pack://application:,,,/KinonekoSoftware.Studio.Views;component/Pages/Instances/Icon.xaml", UriKind.RelativeOrAbsolute);
                _Icon = new ResourceDictionary { Source = uri };
            }

            if (value is DataPartTypeSelector dptS)
            {
                return _Icon?[dptS.Icon] as Geometry;
            }

            if (value is not DataPartType type)
            {
                type = DataPartType.Basic_Custom;
            }

            if (type == DataPartType.InternalSetting)
            {
                _setting = _Icon?["Setting"] as Geometry;
                return _setting;
            }

            if (!_IconDictionary.TryGetValue(type, out var icon))
            {
                icon = DataPart.GetIconName(type);

                if (string.IsNullOrEmpty(icon))
                {
                    return null;
                }
                
                _IconDictionary.Add(type, icon);
            }


            return _Icon?[icon] as Geometry;
        }
    }
}